//
//  ZCXPetpictureDetialViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/19.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXPetpictureDetialViewController.h"
#import "ZCXPetPictureDetialTableViewCell.h"
#import "ZFHYNetWork.h"
#import "ZCXPetPictureDetialModel.h"
#import "ZCXPetPictureView.h"
#import <SVProgressHUD.h>
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface ZCXPetpictureDetialViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UIActivityIndicatorView *activity;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *userArray;
@property (nonatomic, assign) CGFloat secctionHeigth;
@end
@implementation ZCXPetpictureDetialViewController
- (void)dealloc{
    [_urlString release];
    [_activity release];
    [_heigth release];
    [_count release];
    [_feel release];
    [_width release];
    [_tableView release];
    [_userArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.userArray = [NSMutableArray array];
    self.view.backgroundColor =[UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
     self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 * HEIGHT) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    //
    [self.tableView registerClass:[ZCXPetPictureDetialTableViewCell class] forCellReuseIdentifier:@"zcxpetpicturedetialtableview"];
    //
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableviewcellpet"];
    [self.view addSubview:_tableView];
    [_tableView release];
    [self loadDataWithString:[NSString stringWithFormat:@"http://isub.snssdk.com/2/data/v4/get_comments/?group_id=%@&count=20&offset=0&sort=recent&iid=3254973844", self.urlString]];
    CGFloat hh = [self.heigth floatValue]/ [self.width floatValue];
    self.secctionHeigth = hh * (self.view.frame.size.width - 10);
}
- (void)loadDataWithString:(NSString *)urlString{
    [ZFHYNetWork handleGETWithUrlString:urlString parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *dicc in array) {
            ZCXPetPictureDetialModel *pet = [ZCXPetPictureDetialModel ZCXPetPictureDetialModelWithDictionary:dicc];
            [self.userArray addObject:pet];
        }
        [self.tableView reloadData];
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.userArray.count == 0) {
        return 1;
    }else{
        return  self.userArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.userArray.count == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableviewcellpet" forIndexPath:indexPath];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
        label.text = @"<还没有评论>";
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor grayColor];
        [cell addSubview:label];
        [label release];
        return cell;
    }else{
    ZCXPetPictureDetialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zcxpetpicturedetialtableview" forIndexPath:indexPath];
    cell.selectionStyle = 0;
    cell.pet = self.userArray[indexPath.row];
    return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80 * HEIGHT;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ZCXPetPictureView *petView = [[ZCXPetPictureView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, _secctionHeigth + 60 * HEIGHT)];
    petView.pic = self.count;
    petView.titlen = self.feel;
    return petView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return _secctionHeigth + 60 * HEIGHT;
}
- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
